/**
 * 
 */
package org.jackysoft.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author qujiakang 简化的日期类型 继承自
 * @see java.util.Calendar
 * @since 1.0
 */
public class DateUtils extends org.apache.commons.lang.time.DateUtils {

	public static Date getDate() {
		return Calendar.getInstance().getTime();
	}

	public static Calendar compoiseCalendar(int year, int month, int date) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year < 0 ? 1900 : year);
		cal.set(Calendar.MONTH, month <= -1 ? 0 : month - 1);
		cal.set(Calendar.DATE, date <= 0 ? 1 : date);
		return cal;
	}

	/**
	 * 获取年数
	 * */
	public static Integer getYear(Calendar calr) {
		Calendar cal = calr == null ? Calendar.getInstance() : calr;
		return cal.get(Calendar.YEAR);
	}

	/**
	 * 获取月数
	 * */
	public static Integer getMonth(Calendar calr) {
		Calendar cal = calr == null ? Calendar.getInstance() : calr;
		return cal.get(Calendar.MONTH);
	}

	/**
	 * 获取日期中的天数
	 * */
	public static Integer getDate(Calendar calr) {
		Calendar cal = calr == null ? Calendar.getInstance() : calr;
		return cal.get(Calendar.DATE);
	}

	/**
	 * 获取星期数
	 * */
	public static Integer getDay(Calendar calr) {
		Calendar cal = calr == null ? Calendar.getInstance() : calr;
		return cal.get(Calendar.DAY_OF_WEEK) - 1;
	}

	public static Integer getHour(Calendar calr) {
		Calendar cal = calr == null ? Calendar.getInstance() : calr;
		return cal.get(Calendar.HOUR_OF_DAY);
	}

	public static Integer getMinute(Calendar calr) {
		Calendar cal = calr == null ? Calendar.getInstance() : calr;
		return cal.get(Calendar.MINUTE);
	}

	public static Integer getSecond(Calendar calr) {
		Calendar cal = calr == null ? Calendar.getInstance() : calr;
		return cal.get(Calendar.SECOND);
	}

	public static Integer getMillionSecond(Calendar calr) {
		Calendar cal = calr == null ? Calendar.getInstance() : calr;
		return cal.get(Calendar.MILLISECOND);
	}

	public static Calendar translateDate(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}

	/**
	 * @param String
	 *            dateStr 类似2008-02-06
	 * @return 日历对象
	 */
	public static Calendar parseCalendar(String str) {
		if (!StringUtils.isNotVoid(str))
			return Calendar.getInstance();
		String[] dateinfo = str.split("\\s");
		Calendar cl = Calendar.getInstance(java.util.Locale.SIMPLIFIED_CHINESE);
		boolean isdate = true;
		String[] prefix = dateinfo[0].split("\\-");
		for (String s : prefix) {
			if (!ClassUtils.isNumeric(s)) {
				isdate = false;
				break;
			}
		}
		String[] postfix = new String[] { "0", "0", "0" };
		if (dateinfo.length == 2) {
			postfix = dateinfo[1].split(":");
			if (postfix != null) {
				for (String s : postfix) {
					if (!ClassUtils.isNumeric(s)) {
						isdate = false;
						break;
					}
				}
			} else {
				postfix = new String[] { "0", "0", "0" };
			}
		}
		// 2009-10-10 10:11:23
		if (!isdate)
			throw new RuntimeException("日期字符串 : '" + str + "'至少有一个格式不正确 ");

		cl.set(Calendar.YEAR, Integer.parseInt(prefix[0].trim()));
		cl.set(Calendar.MONTH, Integer.parseInt(prefix[1].trim()) - 1);
		cl.set(Calendar.DATE, Integer.parseInt(prefix[2].trim()));
		cl.set(Calendar.HOUR, Integer.parseInt(postfix[0].trim()));
		cl.set(Calendar.MINUTE, Integer.parseInt(postfix[1].trim()));
		cl.set(Calendar.SECOND, Integer.parseInt(postfix[2].trim()));

		return cl;
	}

	/**
	 * 注意如果此参数为null则返回
	 */
	public static String getShowedDateString(Calendar cal) {
		if (cal == null) {
			cal = Calendar.getInstance();
			cal.set(0, 0, 0, 0, 0, 0);
		}
		return formatDate(cal.getTime());
	}

	public static String formatDate(Date dcal) {
		if (dcal == null)
			return "----:--:--";
		SimpleDateFormat sd = new SimpleDateFormat();
		sd.applyPattern("yyyy-MM-dd HH:mm:ss E");
		return sd.format(dcal);
	}

	/**
	 * @author qujiakang
	 * @return 当前时间的字符串表示 形式:xxxx年xx月xx日xx时xx分xx秒
	 */
	public String getNowDatetime() {
		Calendar cal = Calendar.getInstance();
		return formatDate(cal.getTime());
	}

	/**
	 * @author qujiakang
	 * @return 当前日期的简单表示:年-月-日
	 */
	public String getSimpleDateString() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat sd = new SimpleDateFormat();
		sd.applyPattern("yyyy-MM-dd");
		return sd.format(cal.getTime());
	}

	/**
	 * 求两个日期之间的相差天数
	 * 
	 * @return 两个日期之间相差的天数
	 * */
	public static long getDateDiffer(Calendar c1, Calendar c2) {
		long margin = 0;
		margin = (c2.getTimeInMillis() - c1.getTimeInMillis())
				/ (1000 * 60 * 60 * 24);
		return margin;
	}

	public static Calendar getFirstOfThisyear() {
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.set(cal.get(Calendar.YEAR), 1, 1, 0, 0, 0);
		return cal;
	}

	/**
	 * 获取所给或者当前时间的季度数
	 * 
	 * @return 季度数,从0开始第一季度
	 * */
	public static int getQuarter(Calendar cal) {
		cal = cal == null ? Calendar.getInstance() : cal;
		int month = cal.get(Calendar.MONTH);
		return Math.round(new Integer(month % 3).floatValue());
	}

	/**
	 * 某天所在周的第一天
	 * */
	public static Date getFirstDayofWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
		return cal.getTime();
	}

	/**
	 * 获取某天所在周的最后一天
	 * */
	public static Date getLastDayofWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 6);
		return cal.getTime();
	}

}
